﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Liquid;

namespace Common.Controls
{
    public class DialogHelper
    {
        #region DialogBoxes
        private static DialogHelper _dialogHelper = null;
        public DialogHelper()
        {
        }
        
        public void Alert(string Title, string ErrorMessage, object DataContext, Grid RootGrid)
        {
            int BeginningIndex = RootGrid.Children.Count;
            Canvas BackGround = new Canvas();
            BackGround.Background = new SolidColorBrush(Colors.Black);
            BackGround.Opacity = .7;
            BackGround.SetValue(Grid.RowProperty, 0);            
            RootGrid.Children.Add(BackGround);

            Canvas myErrorCanvas = new Canvas();
            myErrorCanvas.Background = new SolidColorBrush(Colors.Transparent);
            myErrorCanvas.SetValue(Grid.RowProperty, 0);            
            RootGrid.Children.Add(myErrorCanvas);

            Dialog myDialog = new Dialog();
            myDialog.Width = 400;
            myDialog.Height = 100;
            myDialog.IsModal = true;
            //myDialog.Resizable = false;
            myDialog.Title = Title;
            myDialog.DataContext = DataContext;
            TextBlock Message = new TextBlock();
            Message.Width = 390;
            Message.Text = ErrorMessage;
            Message.TextWrapping = TextWrapping.Wrap;
            Message.Margin = new Thickness(5);
            myDialog.Content = Message;
            myDialog.Closed += new DialogEventHandler(delegate(object sender, DialogEventArgs e)
            {                
                if (myDialog.Result == DialogButtons.OK)
                {
                    if (this.DialogAccepted != null)
                    {
                        DialogAccepted(myDialog.DataContext);
                    }
                }                
                RootGrid.Children.Remove(RootGrid.Children[BeginningIndex+1]);
                RootGrid.Children.Remove(RootGrid.Children[BeginningIndex]);
            });
            myErrorCanvas.Children.Add(myDialog);
            myDialog.SetValue(Canvas.TopProperty, RootGrid.ActualHeight / 2 - myDialog.Height / 2);
            myDialog.SetValue(Canvas.LeftProperty, RootGrid.ActualWidth / 2 - myDialog.Width / 2);
            myDialog.Show();
        }
        public delegate void DialogAcceptedDelegate(object DataContext);
        public event DialogAcceptedDelegate DialogAccepted;        
        #endregion

        #region Login
        public delegate void LoginCompletedDelegate(bool LoginSuccess, int UserId);
        public event LoginCompletedDelegate LoginCompleted;
        public string Username { get; set; }
        public string Password { get; set; }
        public Guid _studyId;
        int _userId;
        public void ForceLogin(Grid MainGrid, Application App, Guid StudyId)
        {
            _studyId = StudyId;
            int GridChildrenCount = MainGrid.Children.Count;
            Canvas BackGround = new Canvas();
            BackGround.Background = new SolidColorBrush(Colors.Black);            
            BackGround.SetValue(Grid.RowProperty, 0);
            MainGrid.Children.Add(BackGround);

            Canvas myErrorCanvas = new Canvas();
            myErrorCanvas.Background = new SolidColorBrush(Colors.Transparent);
            myErrorCanvas.SetValue(Grid.RowProperty, 0);
            MainGrid.Children.Add(myErrorCanvas);

            Login myLogin = new Login(StudyId);
            myLogin.LoginCompleted += new Login.LoginCompletedDelegate(delegate(bool Success, int UserId)
            {                
                //Runs when the user is successfully logged in
                MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount + 1]);
                MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount]);
                LoginCompleted(true,UserId);                
            });
            myLogin.CreateAccountEvent += new Login.CreateAccountDelegate(delegate()
            {
                //Runs when user clicks option to create a new account
                int GridChildrenCount2 = MainGrid.Children.Count;
                Canvas BackGround2 = new Canvas();
                BackGround2.Background = new SolidColorBrush(Colors.Black);
                BackGround2.SetValue(Grid.RowProperty, 0);
                MainGrid.Children.Add(BackGround2);

                Canvas myErrorCanvas2 = new Canvas();
                myErrorCanvas2.Background = new SolidColorBrush(Colors.Transparent);
                myErrorCanvas2.SetValue(Grid.RowProperty, 0);
                MainGrid.Children.Add(myErrorCanvas2);

                CreateAccount createAccountForm = new CreateAccount();
                createAccountForm.AcountCreatedOrCancelled += new 
                    CreateAccount.AccountCreatedOrCancelledDelegate(delegate(CreateAccount.Result myResult)
                {                    
                    switch (myResult)
                    {
                        case CreateAccount.Result.Created:
                            //Cleaup the controls
                            MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2 + 1]);
                            MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2]);
                            Alert("Account Created", "Your account has been created. You can now login with your new account", null, MainGrid);
                            break;
                        case CreateAccount.Result.Cancelled:
                            //Cleaup the controls
                            MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2 + 1]);
                            MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2]);
                            break;
                        case CreateAccount.Result.UsernameExists:
                            Alert("Account Exists", "An account already exists with this username. Please select another username.", null, MainGrid);
                            break;
                    }
                });
                createAccountForm.SetValue(
                    Canvas.TopProperty, App.Host.Content.ActualHeight / 2 - 110);
                createAccountForm.SetValue(
                    Canvas.LeftProperty, App.Host.Content.ActualWidth / 2 - createAccountForm.Width / 2);
                myErrorCanvas2.Children.Add(createAccountForm);
                createAccountForm.Focus(); 

            });
            myLogin.ValidateClaims += new Login.ValidateClaimsDelegate(delegate(string LoginName, int UserId)
            {
                //This runs after a successful login when claims are not enough to proceed
                int GridChildrenCount2 = MainGrid.Children.Count;
                Canvas BackGround2 = new Canvas();
                BackGround2.Background = new SolidColorBrush(Colors.Black);
                BackGround2.SetValue(Grid.RowProperty, 0);
                MainGrid.Children.Add(BackGround2);

                Canvas myErrorCanvas2 = new Canvas();
                myErrorCanvas2.Background = new SolidColorBrush(Colors.Transparent);
                myErrorCanvas2.SetValue(Grid.RowProperty, 0);
                MainGrid.Children.Add(myErrorCanvas2);

                ValidateClaims claims = new ValidateClaims(_studyId, LoginName,UserId);
                claims.ClaimsCompleted += new ValidateClaims.ClaimsCompletedDelegate(delegate(ValidateClaims.ClaimStatus Status)
                {
                    //Cleaup the controls
                    MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2 + 1]);
                    MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount2]);
                    if (Status == ValidateClaims.ClaimStatus.Completed)
                    {
                        //Runs when the user is successfully logged in
                        MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount + 1]);
                        MainGrid.Children.Remove(MainGrid.Children[GridChildrenCount]);                        
                        LoginCompleted(true, UserId);
                    }
                    else
                    {

                    }
                });
                claims.SetValue(
                   Canvas.TopProperty, App.Host.Content.ActualHeight / 2 - 300);
                claims.SetValue(
                    Canvas.LeftProperty, App.Host.Content.ActualWidth / 2 - claims.Width / 2);
                myErrorCanvas2.Children.Add(claims);
                //claims.Focus();
            });
            myLogin.SetValue(Canvas.TopProperty, App.Host.Content.ActualHeight / 2 - 200 / 2);
            myLogin.SetValue(Canvas.LeftProperty, App.Host.Content.ActualWidth / 2 - myLogin.Width / 2);
            myErrorCanvas.Children.Add(myLogin);
            myLogin.Focus();            
        }

        #endregion
    }
}
